package com.codejoys.monitor.security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class SysAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    private static final Logger logger = LoggerFactory.getLogger(SysAuthenticationSuccessHandler.class);

    @Autowired
    JwtTokenProvider tokenProvider;

    @Override
    public void onAuthenticationSuccess(
            HttpServletRequest request,
            HttpServletResponse response,
            Authentication authentication) throws IOException, ServletException {

        logger.info("登录成功：{}", authentication);

        // 生成jwt token，放入请求头
        tokenProvider.insertAuthenticationToken(response, authentication);

        response.setStatus(HttpServletResponse.SC_OK);

        // 默认登录成功跳转到首页
        // response.sendRedirect("/");
    }
}
